package com.ccdt.tv.roundprogram.round;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ccdt.tv.roundprogram.Adapter.ProgramAdapter;
import com.ccdt.tv.roundprogram.R;
import com.ccdt.tv.roundprogram.base.BaseActivity;
import com.ccdt.tv.roundprogram.bean.ChannelRoot;
import com.ccdt.tv.roundprogram.media.IjkVideoView;
import com.ccdt.tv.roundprogram.view.custom.MyOnItemSelectedListener;
import com.ccdt.tv.roundprogram.view.custom.MyOnKeyAdapter;

/**
 * create by hfq 2018/10/15
 */
public class RoundActivity extends BaseActivity implements RoundContract.View {

    private RelativeLayout rv_group;
    private IjkVideoView video_view;
    private ListView lv_content;
    private ProgramAdapter mProgramAdapter;
    private RoundContract.Presenter mPresenter;

    private int mCurrentPlayIndex = 0;
    private int mSelectedIndex = 0;

    //TODO 可能需要传值
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, RoundActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_round);
        initView();
        mPresenter = new RoundPresenter(this);
        mPresenter.loadChannel();
    }

    private void initView() {
        rv_group = findViewById(R.id.rv_group);
        video_view = findViewById(R.id.video_view);
        lv_content = findViewById(R.id.lv_content);
    }

    @Override
    public void setChannel(ChannelRoot channelRoot) {
        mProgramAdapter = new ProgramAdapter(channelRoot.getChannel(), this);
        lv_content.setAdapter(mProgramAdapter);
        lv_content.requestFocus();
        setListListener(lv_content);
    }

    private void setListListener(ListView listView) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                LogUtils.eTag("CLICK", "当前position : " + position);
                mPresenter.sendHideMsg();
                mPresenter.turnOverProgram(position, mCurrentPlayIndex != position);
                mCurrentPlayIndex = position;
            }
        });
        listView.setOnItemSelectedListener(new MyOnItemSelectedListener() {
            @Override
            public void onItemSelected(BaseAdapter adapter, int position, long id) {
                mPresenter.sendHideMsg();
                mSelectedIndex = position;
            }
        });
    }

    @Override
    public IjkVideoView getVideoView() {
        return video_view;
    }

    @Override
    public void showChannelView() {
        setListSelection(mCurrentPlayIndex);
        if (rv_group.getVisibility() == View.GONE) {
            rv_group.setAnimation(AnimationUtils.loadAnimation(RoundActivity.this, R.anim.slide_in));
            rv_group.setVisibility(View.VISIBLE);
            lv_content.requestFocus();
        }
    }

    @Override
    public void hideChannelView() {
        if (rv_group.getVisibility() == View.VISIBLE) {
            rv_group.setAnimation(AnimationUtils.loadAnimation(RoundActivity.this, R.anim.slide_out));
            rv_group.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean channelViewIsHided() {
        return rv_group.getVisibility() == View.GONE;
    }

    @Override
    public void setListSelection(int position) {
        lv_content.setSelection(position);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onViewDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.eTag("KEY", keyCode);
        switch (keyCode) {
            //OK键
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
                mPresenter.sendHideMsg();
                if (channelViewIsHided()) {
                    showChannelView();
                } else {
                    lv_content.requestFocus();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                if (channelViewIsHided()) {
                    if (mCurrentPlayIndex <= 0) {
                        //循环到最后一个
                        mCurrentPlayIndex = mProgramAdapter.getCount() - 1;
                        lv_content.setSelection(mCurrentPlayIndex);
                        mPresenter.turnOverProgram(mCurrentPlayIndex, true);
                    } else {
                        mCurrentPlayIndex--;
                        lv_content.setSelection(mCurrentPlayIndex);
                        mPresenter.turnOverProgram(mCurrentPlayIndex, true);
                    }
                } else {
                    if (mSelectedIndex == 0) {
                        mSelectedIndex = mProgramAdapter.getCount() - 1;
                        lv_content.setSelection(mSelectedIndex);
                        mPresenter.sendHideMsg();
                    } else {
                        mSelectedIndex--;
                        lv_content.setSelection(mSelectedIndex);
                        mPresenter.sendHideMsg();
                    }
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if (channelViewIsHided()) {
                    if (mCurrentPlayIndex >= mProgramAdapter.getCount() - 1) {
                        //循环到第一个
                        mCurrentPlayIndex = 0;
                        lv_content.setSelection(mCurrentPlayIndex);
                        mPresenter.turnOverProgram(mCurrentPlayIndex, true);
                    } else {
                        mCurrentPlayIndex++;
                        lv_content.setSelection(mCurrentPlayIndex);
                        mPresenter.turnOverProgram(mCurrentPlayIndex, true);
                    }
                } else {
                    if (mSelectedIndex == mProgramAdapter.getCount() - 1) {
                        mSelectedIndex = 0;
                        lv_content.setSelection(mSelectedIndex);
                        mPresenter.sendHideMsg();
                    } else {
                        mSelectedIndex++;
                        lv_content.setSelection(mSelectedIndex);
                        mPresenter.sendHideMsg();
                    }
                }
                break;
            case KeyEvent.KEYCODE_BACK:
                if (!channelViewIsHided()) {
                    hideChannelView();
                } else {
                    return super.onKeyDown(keyCode, event);
                }
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

}
